﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Data;

namespace SF.Base.Core {
	/// <summary>
	/// 用户操作类
	/// </summary>
	public class UserRules {

		#region 用户注册 public static int RegisterUser(string name, string password)
		
		/// <summary>
		/// 用户注册
		/// </summary>
		/// <param name="name"></param>
		/// <param name="password"></param>
		/// <returns></returns>
		public static int RegisterUser(string name, string password) {
			string sql = " insert into dt_users (login,pass) values (@name,@password); ";

			IDataParameter[] idataparameters = {
				Sh.GetParameter("@name",DbType.String,name),
				Sh.GetParameter("@password",DbType.String,password)
			};

			try {
				int ret = Sh.ExecuteNonQuery(
					BaseSettings.Instance.ConnSF,
					CommandType.Text,
					sql, idataparameters
				);
			}
			catch (System.Exception e) {
				Logger.Exception("用户注册失败", e);
				return -160206;
			}

			return 0;
		}

		#endregion

		#region 更新登录密码 public static int UpdatePass(string npwd, long uid, string pass)

		/// <summary>
		/// 更新登录密码
		/// </summary>
		/// <param name="npwd">新密码</param>
		/// <param name="uid">用户数字ID</param>
		/// <param name="pass">旧密码</param>
		/// <returns></returns>
		public static int UpdatePass(string npwd, long uid, string pass) {
			try {
				int ret = Sh.ExecuteNonQuery(
					BaseSettings.Instance.ConnSF,
					CommandType.Text,
					" update dt_users set pass = @npass where uid =@uid and pass = @pass; ",
					Sh.GetParameter("@npass", DbType.String, npwd),
					Sh.GetParameter("@uid", DbType.Int32, uid),
					Sh.GetParameter("@pass", DbType.String, pass)
				);
				if (ret != 1) {
					return -160225;
				}
				return ret;
			}
			catch (System.Exception e) {
				Logger.Exception("更新登录密码失败", e);
				return -160225;
			}
		}

		#endregion

		#region 更新登录邮箱 public static int UpdateLogin(string email, long uid, string pass)

		/// <summary>
		/// 更新登录邮箱
		/// </summary>
		/// <param name="email">新的登录邮箱</param>
		/// <param name="uid">用户数字ID</param>
		/// <param name="pass">密码</param>
		/// <returns></returns>
		public static int UpdateLogin(string email, long uid, string pass) {
			try {
				int ret = Sh.ExecuteNonQuery(
					BaseSettings.Instance.ConnSF,
					CommandType.Text,
					" update dt_users set login = @login where uid =@uid and pass = @pass; ",
					Sh.GetParameter("@login", DbType.String, email),
					Sh.GetParameter("@uid", DbType.Int32, uid),
					Sh.GetParameter("@pass", DbType.String, pass)
				);
				if (ret != 1) {
					return -160221;
				}
				return ret;
			}
			catch (System.Exception e) {
				Logger.Exception("更新昵称失败", e);
				return -160221;
			}
		}

		#endregion

		#region 更新昵称 public static int UpdateNiceName(string nicename, long uid)

		/// <summary>
		/// 更新昵称 UpdateNiceName(string nicename, long uid)
		/// </summary>
		/// <param name="nicename"></param>
		/// <param name="uid"></param>
		/// <returns></returns>
		public static int UpdateNiceName(string nicename, long uid) {
			try {
				int ret = Sh.ExecuteNonQuery(
					BaseSettings.Instance.ConnSF,
					CommandType.Text,
					" update dt_users set nicename = @nicename where uid =@uid; ",
					Sh.GetParameter("@nicename", DbType.String, nicename),
					Sh.GetParameter("@uid", DbType.Int32, uid)
				);
				if (ret != 1) {
					return -160219;
				}
				return ret;
			}
			catch (System.Exception e) {
				Logger.Exception("更新昵称失败", e);
				return -160218;
			}
		}

		#endregion

		#region 根据昵称查询用户ID public static int GetUserIdByNiceName(string nicename)

		/// <summary>
		/// 根据昵称查询用户ID
		/// </summary>
		/// <param name="nicename">昵称</param>
		/// <returns></returns>
		public static int GetUserIdByNiceName(string nicename) {

			try {
				object o = Sh.ExecuteScalar(
					BaseSettings.Instance.ConnSF,
					CommandType.Text,
					"select user_id from dt_users where [nicename] = @name",
					Sh.GetParameter("@name", DbType.String, nicename));

				if (o == null) {
					return 0;
				}
				return int.Parse(o.ToString());

			}
			catch (System.Exception e) {
				Logger.Exception("根据昵称查询用户ID失败", e);
				return -160217;
			}
		}

		#endregion

		#region 根据用户ID查询用户资料 public static DT_USER GetUserByUserId(string userid)

		public static UserEntity GetUserByUserId(string userId) {
			UserEntity user = null;

			try {
				using (
					IDataReader dataReader = Sh.ExecuteReader(
						BaseSettings.Instance.ConnSF,
						CommandType.Text,
						@"
							select 
								uid,
								user_id,
								login,
								pass,
								nicename,
								realname,
								email,
								url,
								registered,
								activationkey,
								status
							from dt_users where user_id = @user_id",
						Sh.GetParameter("@user_id", DbType.String, userId)
					)
				) {
					if (dataReader.Read()) {
						user = BuildEntity.BuildUserEntity(dataReader);
					}
				}
			}
			catch (System.Exception e) {
				Logger.Exception("用户登录查询失败", e);
			}

			if (user == null) return null;

			user.UserMeta = MetaRules.GetUserMeta(userId);

			user.Avatar = user.UserMeta["Avatar"];

			return user;
		}

		#endregion

		#region 根据登录名查询用户资料 public static DT_USER GetUserByLogin(string name)

		/// <summary>
		/// 根据用户查询用户资料
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public static UserEntity GetUserByLogin(string name) {
			UserEntity user = null;

			#region 查询用户主表 DT_USERS

			try {
				using (
					IDataReader dataReader = Sh.ExecuteReader(
						BaseSettings.Instance.ConnSF,
						CommandType.Text,
						@"
							select 
								uid,
								user_id,
								login,
								pass,
								nicename,
								realname,
								email,
								url,
								registered,
								activationkey,
								status
							from dt_users 
							where [login] = @login
						",
						Sh.GetParameter("@login", DbType.String, name)
					)
				) {
					if (dataReader.Read()) {
						user = BuildEntity.BuildUserEntity(dataReader);
					}
				}
			}
			catch (System.Exception e) {
				Logger.Exception("用户登录查询失败", e);
				throw new Exception("用户登录查询失败", e);
			}

			#endregion

			//如何用户不存在 返回NULL
			if (user == null) return null;

			user.UserMeta = MetaRules.GetUserMeta(user.UserId);

			return user;
		}

		#endregion

		
	}
}
